package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.entity.dto.MenuDTO;
import com.enbatis.mybatisplugs.entity.vo.SysResourceVO;
import com.enbatis.mybatisplugs.entity.vo.SysRoleVO;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.service.SysResourceService;
import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.SysResource;


import java.util.List;

@RestController
@RequestMapping("v1/sys_resource")
public class SysResourceController extends BaseController {

@Autowired
private SysResourceService sysResourceService;


    @PostMapping("/add")
    public ResultReturn<SysResource> add(@RequestBody SysResource sysResource){
       if (sysResource.getPid()==null){
           sysResource.setPid(0L);
           sysResource.setUid(this.getAccount().getId());
       }



        return  success(sysResourceService.addResource(sysResource));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(sysResourceService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody SysResource sysResource){
        sysResource.setId(id);
        return success(sysResourceService.updateResourceById(sysResource,getAccount()));
    }


    @GetMapping("/{id}")
    public ResultReturn<SysResource> get(@PathVariable("id") Long id) {
        return success(sysResourceService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<SysResource>> getList(SysResource sysResource){
        return success(sysResourceService.list(new Wrapper<>(sysResource)));
    }


    @GetMapping("/list_menu")
    public ResultReturn<List<MenuDTO>> getMenuList(SysResource sysResource){
        return success(sysResourceService.getMenuList(sysResource,getAccount()));
    }


    @PostMapping("/list_menu_by_role")
    public ResultReturn<List<MenuDTO>> getMenuVOList(@RequestBody SysResourceVO vo){
        return success(sysResourceService.getMenuVOList(vo));
    }


    @GetMapping("/list_parent")
    public ResultReturn<List<SysResource>> getListParent(){
        return success(sysResourceService.list(new Wrapper<>(new SysResource()).eq("pid",0)));
    }




    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page_vo")
    public ResultReturn<Pages<SysResourceVO>> getPage(SysResourceVO resourceVO){
        Page<SysResourceVO> page= getPage();
        return success(sysResourceService.pageVO(page,resourceVO));
    }

}
